var rootview = ui("$");
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var openHelper = require("helper/open");
var http = require("helper/http");
var circleUrl = require("url/circle");


//图片展示gridView
var do_GridView_1 = ui("do_GridView_1");
var detailDescribe = ui("detailDescribe");
var InforZan = ui("InforZan");
rootview.setMapping({
	"UserName.text" : "creatorName",
    "UserPic.tag" : "creatorId",
    "UserPic.source" : "fullCreatorImg",
    "detailDescribe.text" : "content",
    "InforTime.text" : "cnNewAt",
    "InforReply.text" : "commentCont",
    "InforZan.text" : "praiseCont",
    "do_GridView_1.tag" : "bigImgItems",
    "do_GridView_1.items" : "imgItems",
    "do_GridView_1.visible" : "hasImg",
    "do_ALayout_4.tag" : "id",
    "buttonTop.tag" : "isTop"
});

rootview.on("dataRefreshed", function(){
	resetTopBtnStatus();
});

function resetTopBtnStatus(){
	var isTop = ui("buttonTop").tag;
	if(isTop =="1"){
		ui("buttonTop").text = "取消置顶"
	}else{
		ui("buttonTop").text = "设为置顶"
	}
}
//图片浏览注册事件
do_GridView_1.on("touch", function(data){
	openHelper.open("source://view/reuse/imgBrowser.ui", {
		imgDatas :do_GridView_1.tag,index:data
	});
});

//点击个人头像-他的人个主页
ui("alayoutUserInfo").on("touch", function(data, e) {
	var userId = ui("UserPic").tag;
	if(userId !=null){
		openHelper.openPersonal(userId);
	}
	
});

ui("buttonTop").on("touch",function(){
	var isTop = ui("buttonTop").tag;
	if(isTop =="1"){
		undoTop4Topic();
	}else{
		doTop4Topic();
	}
});

function doTop4Topic(){
	var content = ui("detailDescribe").text;
	if(content == null || content.length ==0 || content==""){
		nf.toast("该话题没有文字描述!");
		return;
	}
	var topicId = ui("do_ALayout_4").tag;
	page.fire("to_select_topic_tag", topicId);
}

/**
 * data: {topicId: topicId4toTop, tagName: tagName}
 */
page.on("select_topic_tag_back", function(data) {
	deviceone.print("select_topic_tag_back");
	var topicId = data.topicId;
	var tagName = data.tagName;
	if(topicId == ui("do_ALayout_4").tag){//如果是本 cell操作的
		var params = {
				id: ui("do_ALayout_4").tag,
				tag: tagName				
			};
		http.post(circleUrl.url.topic.doTop4Topic, params, function(data) {
			ui("buttonTop").tag = 1;
			resetTopBtnStatus();
			nf.toast("置顶...完成");
			//page.fire("doTop4Topic",ui("do_ALayout_4").tag);
		});
	}
});
function undoTop4Topic(){
	var params = {
		id: ui("do_ALayout_4").tag
	};
	http.post(circleUrl.url.topic.undoTop4Topic, params, function(data) {
		ui("buttonTop").tag = 2;
		resetTopBtnStatus();
		nf.toast("取消置顶...完成");
		//page.fire("doTop4Topic",ui("do_ALayout_4").tag);
	});
}

